package com.relax.readWriteLock;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockDemo {

    ReadWriteLock lock = new ReentrantReadWriteLock();
    Lock readLock = lock.readLock();
    public static void main(String[] args) {
        ReadWriteLockDemo readWriteLockDemo = new ReadWriteLockDemo();
        new Thread(()->{readWriteLockDemo.read();}).start();
        new Thread(()->{readWriteLockDemo.read();}).start();




    }
    private void read()  {
        readLock.lock();
        System.out.println(Thread.currentThread().getName()+"获取readLock...开始执行方法");
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"获取readLock...方法结束");
        readLock.unlock();
    }
}
